package com.sky.business.sys.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.business.sys.param.CameraConfigParam;
import com.sky.common.api.Result;
import com.sky.common.docker.exception.CmccApiCallException;
import com.sky.common.http.HttpDecorate;
import com.sky.common.http.HttpDecorateBuilder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.*;

@Slf4j
@RestController
@Tag(name = "zlm回调", description = "zlm回调")
@RequestMapping("/hook")
@Validated
public class ZlmHookController {

    @Data
    public static class OnStreamNotFound {
        private String mediaServerId;
        private String app;
        private String id;
        private String ip;
        private String params;
        private Integer port;
        private String schema;
        private String protocol;
        private String stream;
        private String vhost;
    }
    public static HttpDecorate httpDecorator = HttpDecorateBuilder.create().pool(10, 5).disableCookieManagement().build();
    public static ObjectMapper objectMapper = new ObjectMapper().setTimeZone(SimpleTimeZone.getTimeZone("GMT+8"));



    @PostMapping(value = "/on_stream_not_found", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object onStreamNotFound(@RequestBody OnStreamNotFound onStreamNotFound) throws IOException {
        System.out.println(onStreamNotFound);
        String rtmpUrl = "rtmp://172.20.6.25/"+onStreamNotFound.getApp()+"/"+onStreamNotFound.getStream();
        String  addStreamProxyUrl = "http://172.22.2.235/index/api/addStreamProxy";
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("secret", "9Mqat287ug5bxQD6I7gKpueas2S9m82P"));
        params.add(new BasicNameValuePair("vhost", "__defaultVhost__"));
        params.add(new BasicNameValuePair("app", onStreamNotFound.app));
        params.add(new BasicNameValuePair("stream", onStreamNotFound.stream));
        params.add(new BasicNameValuePair("url", rtmpUrl));
        params.add(new BasicNameValuePair("rtp_type", "0"));
        params.add(new BasicNameValuePair("retry_count", "-1"));
        params.add(new BasicNameValuePair("time_sec", "3"));
        params.add(new BasicNameValuePair("enable_hls", "false"));
        params.add(new BasicNameValuePair("enable_hls_fmp4", "false"));
        params.add(new BasicNameValuePair("enable_mp4", "false"));
        params.add(new BasicNameValuePair("enable_rtsp", "true"));
        params.add(new BasicNameValuePair("enable_rtmp", "false"));
        params.add(new BasicNameValuePair("enable_ts", "false"));
        params.add(new BasicNameValuePair("enable_fmp4", "false"));

        // 4. 将参数列表包装成 UrlEncodedFormEntity 并设置字符编码
        // 这将自动设置 Content-Type 为 application/x-www-form-urlencoded
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
        HttpUriRequest request = RequestBuilder.post().setEntity(formEntity)
                .addHeader("Content-Type","application/x-www-form-urlencoded")
                .setUri(addStreamProxyUrl).build();

        String json = httpDecorator.execute(request).result().string();
        System.out.println(json);


        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "success");
        return map;
    }
}